package cn.shengongying.api.controller;

import cn.shengongying.anon.TryGet;
import cn.shengongying.anon.User;
import cn.shengongying.api.dto.CompanyCardInDTO;
import cn.shengongying.api.dto.IdInDTO;
import cn.shengongying.bean.BaseUser;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.model.BossCompanyCard;
import cn.shengongying.model.BossCompanyInfo;
import cn.shengongying.service.IBossCompanyCardService;
import cn.shengongying.service.IBossCompanyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * author: RXK
 * date: 2022/1/7 星期五
 * desc:
 */
@RestController
@Api(tags = "公司信息")
@RequestMapping("/boss/company")
public class CompanyController extends BaseController {


    private final IBossCompanyCardService companyCardService;

    private final IBossCompanyInfoService companyInfoService;

    public CompanyController(IBossCompanyCardService companyCardService, IBossCompanyInfoService companyInfoService) {
        this.companyCardService = companyCardService;
        this.companyInfoService = companyInfoService;
    }

    @ApiOperation(value = "明信片新增/更新")
    @PostMapping("/card/update")
    public ResponseResult<String> card(@ApiIgnore @User BaseUser user, @RequestBody @Validated CompanyCardInDTO inDTO) {
        BossCompanyCard card = inDTO.build(user);
        if (null != card) {
            companyCardService.saveOrUpdate(card);
        }
        return ResponseResult.success();
    }



    @TryGet
    @ApiOperation(value = "明信片信息")
    @PostMapping(value = "/card/info")
    public ResponseResult<BossCompanyCard> cardInfo(@ApiIgnore @User BaseUser user, @RequestBody IdInDTO inDTO) {
        Long userId;
        if (inDTO.getId() != null) {
            userId = inDTO.getId();
        } else if (null != user) {
            userId = user.getId();
        }else{
            return ResponseResult.fail();
        }
        BossCompanyCard card = companyCardService.findByUserId(userId);
        return ResponseResult.success(card);
    }


    @TryGet
    @ApiOperation(value = "基本信息")
    @PostMapping("/info")
    public ResponseResult<BossCompanyInfo> info(@ApiIgnore @User BaseUser user,@RequestBody IdInDTO inDTO) {
        Long userId;
        if (inDTO.getId() != null) {
            userId = inDTO.getId();
        } else if (null != user) {
            userId = user.getId();
        }else{
            return ResponseResult.fail();
        }
        BossCompanyInfo info = companyInfoService.findByBoss(userId);
        return ResponseResult.success(info);
    }


}
